package com.serkol.ams.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.serkol.ams.bean.MyPage;
import com.serkol.ams.entity.Notice;
import com.serkol.ams.service.NoticeService;
import com.serkol.ams.mapper.NoticeMapper;
import com.serkol.handler.MyLambdaQueryWrapper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
* @author Administrator
* @description 针对表【notice】的数据库操作Service实现
* @createDate 2025-02-28 15:40:01
*/
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice>
    implements NoticeService{

    @Override
    public MyPage<Map<String, Object>> findByPageList(int pageSize, int current) {
        MyLambdaQueryWrapper<Notice> queryWrapper=new MyLambdaQueryWrapper<>();
        queryWrapper.select(Notice::getId,Notice::getName,Notice::getImageId,Notice::getStartDate,Notice::getEndDate,Notice::getState,Notice::getRemark);
        queryWrapper.picture(Notice::getImageId).as("url");
        queryWrapper.orderByDesc(Notice::getCreateDate);
        Page<Map<String, Object>> page = new Page<>(current, pageSize);
        IPage<Map<String, Object>> data = this.baseMapper.selectMapsPage(page, queryWrapper);
        return MyPage.parsePage(data);
    }

    @Override
    public boolean byDelete(long id) {
        if(id>0) {
            return this.removeById(id);
        }
        return false;
    }

    @Override
    public List<Map<String, Object>> findByList() {
        MyLambdaQueryWrapper<Notice> queryWrapper=new MyLambdaQueryWrapper<>();
        queryWrapper.select(Notice::getId,Notice::getName,Notice::getImageId,Notice::getRemark);
        queryWrapper.picture(Notice::getImageId).as("url");
        queryWrapper.orderByDesc(Notice::getCreateDate);
        return this.listMaps(queryWrapper);
    }

    @Override
    public Map<String, Object> findByView(String[] list) {
        long l = System.currentTimeMillis();
        MyLambdaQueryWrapper<Notice> queryWrapper=new MyLambdaQueryWrapper<>();
        queryWrapper.select(Notice::getId,Notice::getName,Notice::getImageId,Notice::getRemark);
        queryWrapper.picture(Notice::getImageId).as("url");
        queryWrapper.orderByDesc(Notice::getCreateDate);
        if(list.length>0) {
            queryWrapper.notIn(Notice::getId, list);
        }
        queryWrapper.le(Notice::getStartDate,l).ge(Notice::getEndDate,l);
        queryWrapper.last("limit 1");
        return this.getMap(queryWrapper);
    }
}




